#include "RingQueue.hpp"
#include "Task.hpp"
#include <functional>
#include <unistd.h>

using func_t = Task;

int add(int x, int y) {
    return x + y;
}

void* Consumer(void* args) {
    RingQueue<func_t>* rq = static_cast<RingQueue<func_t>*>(args);
    while (true) {
        func_t func;

        rq->Pop(&func);

        std::cout << "consumer -> " << func.Excute() << std::endl;
    }
}

void* Productor(void* args) {
    srand(time(nullptr) ^ getpid());
    RingQueue<func_t>* rq = static_cast<RingQueue<func_t>*>(args);
    while (true) {
        sleep(1);

        int x = rand() % 10 + 1, y = rand() % 10 + 1;
        func_t func(x, y);
        std::cout << "productor -> x: " << x << " y: " << y << std::endl;
        rq->Push(func);

    }
}

int main() {
    pthread_t c1, c2, p1, p2, p3;
    RingQueue<func_t>* rq = new RingQueue<func_t>();
    pthread_create(&c1, nullptr, Consumer, (void*)rq);
    pthread_create(&c2, nullptr, Consumer, (void*)rq);
    pthread_create(&p1, nullptr, Productor, (void*)rq);
    pthread_create(&p2, nullptr, Productor, (void*)rq);
    pthread_create(&p3, nullptr, Productor, (void*)rq);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    return 0;
}